//package ru.inse.realty.core.service.impl;
//
//import java.util.HashMap;
//import java.util.List;
//import java.util.Map;
//
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//
//import ru.inse.realty.core.dao.IRealtyItemCriteraAppenderFactory;
//import ru.inse.realty.core.dao.IRealtyItemDao;
//import ru.inse.realty.core.domain.RealtyItem;
//import ru.inse.realty.core.domain.RealtyObject;
//import ru.inse.realty.core.domain.RealtyRubric;
//import ru.inse.realty.core.service.IRealtyItemService;
//import ru.inse.realty.core.service.filter.IRealtyFilter;
//import ru.inse.realty.core.service.impl.RealtyItemChangedEvent.EventType;
//import ru.xeden.core.common.ComplexKey;
//import ru.xeden.core.common.listeners.IEventListener;
//
//
//public class ItemCounter
//    implements IEventListener<RealtyItemChangedEvent>
//{
//    private static final Logger LOGGER = LoggerFactory.getLogger(ItemCounter.class);
//    private IRealtyItemCriteraAppenderFactory criteraAppenderFactory;
//    private final Map<ComplexKey, Long> map = new HashMap<ComplexKey, Long>();
//    private RealtyObjectService realtyObjectManager;
//    private IRealtyItemService realtyItemService;
//    
//    public ItemCounter(RealtyObjectService realtyObjectManager)
//    {
//        this.realtyObjectManager = realtyObjectManager;
//    }
//
//    public void setRealtyItemService(IRealtyItemService realtyItemService)
//    {
//        this.realtyItemService = realtyItemService;
//    }
//    
//    public Long getItemCount(Long cityId, RealtyRubric rubric, RealtyObject object,
//        IRealtyFilter filter)
//    {
//        return getOrCreate(cityId, rubric, object, filter);
//    }
//
//    private Long getOrCreate(Long cityId, RealtyRubric rubric, RealtyObject object,
//        IRealtyFilter filter)
//    {
//        ComplexKey key = ComplexKey.newInstance(cityId, rubric, object, filter);
//        Long count = map.get(key);
//        if (count == null)
//        {
//            synchronized (map)
//            {
//                count = map.get(key);
//                if (count == null)
//                {
//                    count = realtyItemService.count(criteraAppenderFactory.createCriteriaAppender(rubric,
//                        object, filter == null ? null : filter.getRealtyQueryParam()));
//                    map.put(key, count);
//                }
//            }
//        }
//        return count;
//    }
//
//    private void remove(Long cityId, RealtyRubric rubric, RealtyObject object,
//        List<IRealtyFilter> filters)
//    {
//        synchronized (map)
//        {
//            map.remove(ComplexKey.newInstance(cityId, rubric, object, null));
//            for (IRealtyFilter f : filters)
//            {
//                map.remove(ComplexKey.newInstance(cityId, rubric, object, f));
//            }
//        }
//    }
//
//    @Override
//    public void onEvent(RealtyItemChangedEvent event)
//    {
//        EventType type = event.getType();
//        if (type == EventType.AFTER_DELETE || type == EventType.AFTER_INSERT)
//        {
//            RealtyItem item = event.getRealtyItem();
//            remove(event.getCityId(), item.getRubric(), item.getObject(),
//                realtyObjectManager.getFilters(item.getObject()));
//        }
//    }
//}
